﻿#include "QtPotPlayer.h"
#include "PlayConfig.h"
#include "qevent.h"
#include "qlabel.h"
#include "qpoint.h"
#include "ui_QtPotPlayer.h"

#include <QMessageBox>
#include <QStandardPaths>
#include <QFileDialog>
#include <QTime>
#include <QDir>
#include <QTime>
#include <QMouseEvent>
#include <QTabBar>

QtPotPlayer::QtPotPlayer(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::QtPotPlayer)
{
    ui->setupUi(this);
    Controller = new UIController(this);
    PlayCtrl = new PlayConfig(this);
    
    AddTab = new QToolButton(this);
    AddTab->setObjectName("AddTab");
    AddTab->setText("+");

    ui->TabListWidget->tabBar()->addTab("");
    ui->TabListWidget->tabBar()->setTabEnabled(1,false);
    ui->TabListWidget->tabBar()->setMovable(false);
    ui->TabListWidget->tabBar()->setTabButton(1, QTabBar::RightSide, AddTab);
    // 获取指定索引的tab标签
    connect(AddTab,&QPushButton::clicked,[=](){
        static int i = 0;
        QString str = "这是第";
        QLabel *label = new QLabel;
        label->setText(str.append(QString::number(++i)).append("页"));
        label->setStyleSheet("background:#333333;color:white");
        ui->TabListWidget->addTab(label,tr("列表").append(QString::number(i)));
    });
//    ui->ListWidget->setStyleSheet("border:1px solid red;");
//    ui->TabListWidget->setCornerWidget(AddTab);
    /*
    \\
    || 播放/暂停 控制按钮
    //
    */
    ui->BtnPlayCtrl->setIcon(QIcon(QPixmap(":/QPStyle/image/play.svg")));
    ui->BtnPlayCtrl->installEventFilter(this);
    connect(ui->BtnPlayCtrl,&QPushButton::clicked,this,&QtPotPlayer::EmitBtnPlayCtrl);
    /*
    \\
    || 停止按钮
    //
    */
    ui->BtnStop->setIcon(QIcon(QPixmap(":/QPStyle/image/stop.svg")));
    ui->BtnStop->installEventFilter(this);
    connect(ui->BtnStop,&QPushButton::clicked,this,&QtPotPlayer::EmitBtnStop);
    /*
    \\
    || 上一曲按钮
    //
    */
    ui->BtnPrevious->setIcon(QIcon(QPixmap(":/QPStyle/image/back.svg")));
    ui->BtnPrevious->installEventFilter(this);
    connect(ui->BtnPrevious,&QPushButton::clicked,this,&QtPotPlayer::EmitBtnPrevious);
    /*
    \\
    || 下一曲按钮
    //
    */
    ui->BtnNext->setIcon(QIcon(QPixmap(":/QPStyle/image/forward.svg")));
    ui->BtnNext->installEventFilter(this);
    connect(ui->BtnNext,&QPushButton::clicked,this,&QtPotPlayer::EmitBtnNext);
    /*
    \\
    || 打开文件选择对话框，选择音视频文件
    //
    */
    ui->BtnOpen->setIcon(QIcon(QPixmap(":/QPStyle/image/ejectUp.svg")));
    ui->BtnOpen->installEventFilter(this);
    connect(ui->BtnOpen,&QPushButton::clicked,this,&QtPotPlayer::EmitBtnOpen);
    /*
    \\
    || 播放设置按钮：如图像RGB调整，均衡器等
    //
    */
    ui->BtnPlayConfig->setIcon(QIcon(QPixmap(":/QPStyle/image/control.svg")));
    ui->BtnPlayConfig->installEventFilter(this);
    connect(ui->BtnPlayConfig,&QPushButton::clicked,this,&QtPotPlayer::EmitBtnPlayConfig);
    /*
    \\
    || 【BtnHideSideWidget】隐藏/显示播放列表，【LabPlayLogo】初始状态logo
    //
    */
    ui->BtnHideSideWidget->setIcon(QIcon(QPixmap(":/QPStyle/image/option.svg")));
    ui->LabPlayLogo->setPixmap(QPixmap(":/QPStyle/image/playLogo.png"));
    connect(ui->BtnHideSideWidget,&QPushButton::clicked,this,&QtPotPlayer::HidePlayList);
    /*
    \\
    || 静音按钮
    //
    */
    ui->BtnVolume->setIcon(QIcon(QPixmap(":/QPStyle/image/Volume.svg")));
    ui->BtnVolume->installEventFilter(this);
//    connect(ui->BtnVolume,&QPushButton::clicked,this,&QtPotPlayer::HidePlayList);
    /*
    \\
    || 叠层窗口【播放窗口/logo窗口】
    //
    */
    ui->stackedWidget->setCurrentIndex(1);
//    ui->stackedWidget->installEventFilter(this);//暂时用不到，后期用于显示和隐藏控制栏
    /*
    \\
    || 控制栏，摆放按钮的父窗口
    //
    */
//    ui->ControlBar->installEventFilter(this);
    
}

QtPotPlayer::~QtPotPlayer()
{
    delete ui;
}


void QtPotPlayer::HidePlayList()
{
    if(ui->SideWidget->isVisible()){
        ui->SideWidget->hide();
        ui->gridLayout->setContentsMargins(3,3,0,0);
    }else{
        ui->SideWidget->show();
        ui->gridLayout->setContentsMargins(3,3,3,0);
    }
}

//播放/暂停 控制按钮
void QtPotPlayer::EmitBtnPlayCtrl()
{
    emit SigBtnPlayCtrlObj(ui->BtnPlayCtrl);  
}
//停止按钮
void QtPotPlayer::EmitBtnStop()
{
    //是传递ui->BtnPlayCtrl，而不是ui->BtnStop
    emit SigBtnStopObj(ui->BtnPlayCtrl);  
}
//上一曲按钮
void QtPotPlayer::EmitBtnPrevious()
{
    emit SigBtnPreviousObj(ui->BtnPrevious);  
}
//下一曲按钮
void QtPotPlayer::EmitBtnNext()
{
    emit SigBtnNextObj(ui->BtnNext);  
}
//打开文件选择对话框，选择音视频文件
void QtPotPlayer::EmitBtnOpen()
{
    emit SigBtnOpenObj(ui->BtnOpen);  
}
//播放设置按钮：如均衡器,图像RGB调整，快进，快退，倍数播放，字幕。。。
void QtPotPlayer::EmitBtnPlayConfig()
{
    PlayCtrl->exec();
//    emit SigBtnPlayConfigObj(ui->BtnPlayConfig);  
}

WId QtPotPlayer::WinId()
{
    return this->winId();
}

void QtPotPlayer::addWidget(int index,QWidget *w)
{
    if((index == 0) || (index == 1))
    {
        ///
        /// \brief QMessageBox::warning 不允许设置索引为0和1，0索引是视频画面显示窗口，1是默认启动窗口的logo
        ///
        QMessageBox::warning(nullptr,tr("错误"),tr("抱歉，索引0和1已被UI提前预留，请更换新的索引值"));
        return;
    }
    ui->stackedWidget->insertWidget(index,w);
}

void QtPotPlayer::setCurrentIndex(int index)
{
    ui->stackedWidget->setCurrentIndex(index);
}

void QtPotPlayer::setCurrentWidget(QWidget *w)
{
    ui->stackedWidget->setCurrentWidget(w);
}

QWidget * QtPotPlayer::CurrentWidget(int index)
{
    return ui->stackedWidget->widget(index);
}
    

bool QtPotPlayer::eventFilter(QObject * obj, QEvent * event)
{
    QMouseEvent *Event = static_cast<QMouseEvent *>(event);
    
    if(obj == ui->BtnPlayConfig)
    {
        if(Event->type() == QMouseEvent::MouseButtonPress)
        {
            QPointF pos = Event->globalPosition();
//            if(PlayCtrl->isVisible())
//            {
                PlayCtrl->move(pos.x()-PlayCtrl->width()  ,pos.y() - PlayCtrl->height() - 50);
//            }
        }
    }
    return QWidget::eventFilter(obj,event);
}

void QtPotPlayer::resizeEvent(QResizeEvent *e)
{
//    int count = ui->TabListWidget->tabBar()->count();
//    int width = ui->TabListWidget->tabBar()->width()/2;
//    QString qss = QString("QTabBar::tab{max-height:%1px;}").arg(25);
//    this->setStyleSheet(qss);
}
